package sctu.edu.java.year2016.team06.project;



import java.io.*;
import java.util.Date;
import java.util.Random;
import java.util.Scanner;


import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;
import sctu.edu.java.year2016.team02.wanggaowen.Pra.Fil;
import sctu.edu.java.year2016.team04.luoxinghua.lianxi.Jiecheng.lian.*;

import java.awt.*;
import java.io.*;
import java.lang.Math;
import java.util.Random;
import java.util.Scanner;
import java.util.Date;

/**
 * Created by LCJ on 2016/6/22.
 */
public class Database {

    Scanner scan = new Scanner(System.in);

    public void Document() throws IOException {                 //被Worker调用，功能：回复客户

        File file = new File("D:/workerwrite.txt");

        if (file.exists()) {
            file.delete();
        }
        file.createNewFile();

        String methed;
        System.out.println("001号您好,请您回复客户问题的解决方案：");
        methed = scan.nextLine();
        DefineFileOutputStream fileOutputStream = new DefineFileOutputStream(file);
        fileOutputStream.write("解决方案：");
        fileOutputStream.write(methed);
        fileOutputStream.close();
    }


    public void Document1() throws IOException {                 //被Worker调用，模拟功能：工作人员将信息录入


        File file = new File("D:/database.txt");
        FileWriter fw = new FileWriter(file, true);               //设置文本可追加
        BufferedWriter bufw = new BufferedWriter(fw);

        String ab[] = new String[10000];
        String abc[] = new String[10000];
        System.out.println("请确认你录入数据库的问题：");


        for (int j = 0; j < 10000; j++) {

            String question;
            String write;
            String writ;

            System.out.println("请录入你的问题：");
            write = scan.nextLine();

            ab[j] = write;
            bufw.write(ab[j]);
            bufw.flush();
            bufw.newLine();

            System.out.println("请录入你的解决方法：");
            question = scan.nextLine();

            abc[j] = question;
            bufw.write(abc[j]);
            bufw.flush();
            bufw.newLine();

            System.out.println("您是否需要继续录入数据：");
            writ = scan.nextLine();

            if (writ.equals("否"))
                break;
            else if (writ.equals("是")) {
                continue;
            } else {
                System.out.println("输入错误！");
            }
        }
        bufw.close();
        fw.close();
    }


    public void questionbase() throws IOException {             //被Worker调用，模拟功能：工作人员将信息录入

        File file = new File("D:/question.txt");
        FileWriter fw = new FileWriter(file, true);
        BufferedWriter bufw = new BufferedWriter(fw);

        String ab[] = new String[10000];

        System.out.println("请将您的解决了的问题关键字录入问题库：");

        for (int j = 0; j < 10000; j++) {

            String write;
            String writ;
            System.out.println("请录入您解决了的问题关键字：");
            write = scan.nextLine();
            ab[j] = write;

            bufw.write(ab[j]);
            bufw.flush();
            bufw.newLine();


            System.out.println("您是否需要继续录入问题：");
            writ = scan.nextLine();
            if (writ.equals("否"))
                break;
            else if (writ.equals("是")) {
                continue;
            } else {
                System.out.println("输入错误！");
            }

        }

        bufw.close();
        fw.close();

    }


    public void meassage() throws IOException {             //被Worker调用，模拟功能：工作人员将信息录入

        File file = new File("D:/meassage.txt");
        FileWriter fw = new FileWriter(file, true);               //设置文本可追加
        BufferedWriter bufw = new BufferedWriter(fw);

        String ab[] = new String[10000];


        for (int j = 0; j < 10000; j++) {


            String write;
            String writ;

            System.out.println("请录入你的留言：");
            write = scan.next();

            ab[j] = write;
            bufw.write(ab[j]);
            bufw.flush();
            bufw.newLine();


            System.out.println("您是否需要继续留言：");
            writ = scan.next();

            if (writ.equals("否")) {
                break;
            } else if (writ.equals("是")) {
                continue;
            } else {
                System.out.println("输入错误！");
            }


        }

    }


    public void meassage1() throws IOException {                //被User调用，模拟功能：读取录入的所有数据

        FileReader fr = new FileReader("D:/meassage.txt");
        BufferedReader bufr = new BufferedReader(fr);

        String s;
        int i = 0;
        while ((s = bufr.readLine()) != null) {

            if(i==0) {
                System.out.println(s);
            }
           if(i>=1) {
               System.out.println(i + "楼：" + s);
           }
            i++;
        }

    }

    public void Document2() throws IOException {                //被User调用，模拟功能：读取录入的所有数据

        FileReader fr = new FileReader("D:/database.txt");
        BufferedReader bufr = new BufferedReader(fr);

        String s = null;
        int m = 1;
        int n = 1;
        int i = 0;

        while ((s = bufr.readLine()) != null) {

            if (i == 0) {
                System.out.println(s);
            }
            if (i > 0 && i % 2 != 0) {

                System.out.println("问题" + m + "：" + s);
                m = m + 1;
            }
            if (i > 0 && i % 2 == 0) {

                System.out.println("解决方法" + n + "：" + s);
                n = n + 1;
            }
            i++;
        }

    }


    public void Document8() throws IOException {                    //被管理人员调用，模拟功能：接收用户反馈

        FileInputStream fileInputStream = new FileInputStream("D:/admin.txt");

        System.out.println("管理员您好！下面是用户反馈：");
        byte byt[] = new byte[10000];
        int length = fileInputStream.read(byt);
        System.out.println(new String(byt, 0, length));
        System.out.println("请您尽快完善系统，让客户满意");
        fileInputStream.close();

    }

    public void Documen() throws IOException {                       //被User调用，模拟功能：查看解决了的问题

        FileReader fr = new FileReader("D:/question.txt");
        BufferedReader bufr = new BufferedReader(fr);


        System.out.println("下面是问题库");
        String s = null;
        int i = 0;

        while ((s = bufr.readLine()) != null) {
            if (i == 0) {
                System.out.println(s);
            } else {

                System.out.println("现有问题" + " （" + i + ")");
                System.out.println(s);
            }
            i++;
        }

    }


    public void Documen1() throws IOException {             //被User调用，模拟功能：搜索单个问题的解决方法

        FileReader fr = new FileReader("D:/database.txt");
        BufferedReader bufr = new BufferedReader(fr);

        String input;

        System.out.println("请您再次输入问题关键字：");
        input = scan.nextLine();

        int k = 0;
        int i = 0;
        String s;

        while ((s = bufr.readLine()) != null) {
            i++;

            if (input.equals(bufr.readLine())) {
                k = i;

            }

            while (i == k) {

                System.out.println("解决方法：\n" + bufr.readLine());

                break;
            }

        }

        fr.close();
        bufr.close();

    }


    public void Document9() throws IOException {          //被User调用，模拟功能：调查表

        File file = new File("D:/admin.txt");

        FileInputStream fileInputStream = new FileInputStream("D:/admin.txt");
        byte byt[] = new byte[10000];
        int length = fileInputStream.read(byt);
        DefineFileOutputStream fileOutputStream = new DefineFileOutputStream(file);
        fileOutputStream.write(new String(byt, 0, length));

        String methed;

        System.out.println("为了更好地服务用户，这儿有一张调查表");
        System.out.println("请你写下宝贵的建议：");
        methed = scan.nextLine();

        fileOutputStream.write(methed);

        System.out.println("谢谢您的配合，再见");
        fileOutputStream.close();
    }


    public void ad() {                                //被User调用， 模拟功能：随机广告

        System.out.println("公益广告：");
        Random random = new Random();

        int i;
        i = random.nextInt(6);
        i = (int) (Math.random() * 6);


        switch (i) {
            case 0:
                System.out.println("保护野生动物");
                break;
            case 1:
                System.out.println("珍惜每一滴水");
                break;
            case 2:
                System.out.println("爱护我们共同的家园-地球");
                break;
            case 3:
                System.out.println("安全第一");
                break;
            case 4:
                System.out.println("珍爱生命");
                break;
            case 5:
                System.out.println("反对恐怖主义");
                break;
            default:
                System.out.println("还没有广告");
        }

    }


    public void register() {                           //被Uer调用， 模拟功能：动态登录

        System.out.println("请输入“获取”得到动态登录账号");
        String get;
        get = scan.nextLine();

        if (get.equals("获取")) {
            Random random = new Random();
            StringBuffer ra = new StringBuffer();

            String sum = "";
            String base = "12345678912345678912345678912345671237";

            for (int j = 0; j < base.length(); j++) {
                int number = random.nextInt(base.length());
                ra.append(base.charAt(number));
            }

            String bas[] = ra.toString().split("");
            for (int k = 0; k < 10; k++) {

                sum = sum + bas[k];

            }

            System.out.println("恭喜您获得动态登录账号：\n" + sum);

            String account;
            account = sum;

            StringBuffer rb = new StringBuffer();
            String sum1 = "";
            String base1 = "abcdefghijklmnopqrstuvwxyz0123456789";

            for (int j = 0; j < base1.length(); j++) {
                int number = random.nextInt(base1.length());
                rb.append(base1.charAt(number));
            }

            String bas1[] = rb.toString().split("");
            for (int k = 0; k < 6; k++) {
                sum1 = sum1 + bas1[k];
            }

            System.out.println("动态登录密码：\n" + sum1);

            String password;

            System.out.println("系统提示：登录密码和登录密码不能为空！");

            password = sum1;

            String logoaccount;
            String logopassword;

            System.out.println("请输入登录账号：");
            logoaccount = scan.nextLine();

            System.out.println("请输入登录密码：");
            logopassword = scan.nextLine();

            if (account.equals("") || password.equals("")) {
                System.out.println("输入错误！");
                System.out.println("程序终止！");
                System.exit(0);
            }

            boolean b1;
            boolean b2;

            b1 = (account.equals(logoaccount));
            b2 = (password.equals(logopassword));

            if (b1 == true && b2 == false)
                System.out.println("密码错误！");

            if (b1 == false && b2 == true)
                System.out.println("账号错误！");

            if (b1 == false && b2 == false)
                System.out.println("账号错误或者密码错误！");

            if (b1 == true && b2 == true) {

                System.out.println("登录成功");

                Database logotime = new Database();
                logotime.logotime();

            } else {

                System.out.println("登录失败！");
                System.exit(0);

            }
        }
    }


    public void enroll() {                           //被Uer调用， 模拟功能：静态登录

        Random random = new Random();
        StringBuffer ra = new StringBuffer();

        String sum = "";
        String base = "012345678901234567890123456789";

        for (int j = 0; j < base.length(); j++) {
            int number = random.nextInt(base.length());
            ra.append(base.charAt(number));
        }

        String bas[] = ra.toString().split("");

        for (int k = 0; k < 10; k++) {
            sum = sum + bas[k];
        }

        System.out.println("恭喜您获得登录账号：\n" + sum);

        File file = new File("D:/enroll.txt");
        File file1 = new File("D:/enroll1.txt");

        try {
            DefineFileOutputStream fileOutputStream = new DefineFileOutputStream(file);
            fileOutputStream.write(sum);
            try {
                fileOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

        String ouput;
        System.out.println("请设置密码：");
        ouput = scan.nextLine();

        try {
            DefineFileOutputStream fileOutputStrea = new DefineFileOutputStream(file1);
            fileOutputStrea.write(ouput);
            try {
                fileOutputStrea.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

        System.out.println("注册成功！");


    }

    public void logotime() {                         //被Uer调用， 模拟功能：登录时间

        Date logotime = new Date();

        System.out.print("登录时间：" + String.format("%tF", logotime) + " " + String.format("%tr", logotime));

       /* System.out.print(String.format("%tF", logotime));
        System.out.println(String.format("%tr", logotime));*/

    }


    public void logo() {                            //被Uer调用， 模拟功能：静态登录

        String account;
        String password;
        String m = "";
        String n = "";

        System.out.println("请输入登录账号：");
        account = scan.nextLine();

        System.out.println("请输入登录密码：");
        password = scan.nextLine();

        FileInputStream fileInputStream = null;

        try {
            fileInputStream = new FileInputStream("D:/enroll.txt");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        byte byt[] = new byte[10000];
        try {
            int length = fileInputStream.read(byt);
            m = (new String(byt, 0, length));
        } catch (IOException e) {
            e.printStackTrace();
        }

        FileInputStream fileInputStre = null;

        try {
            fileInputStre = new FileInputStream("D:/enroll1.txt");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        byte byt1[] = new byte[10000];
        try {
            int length = fileInputStre.read(byt1);
            n = (new String(byt1, 0, length));
        } catch (IOException e) {
            e.printStackTrace();
        }

        boolean a = (account.equals(m));
        boolean b = (password.equals(n));

        if (a == true && b == false)
            System.out.println("密码错误！");

        if (a == false && b == true)
            System.out.println("账号错误！");

        if (a == false && b == false)
            System.out.println("账号错误或者密码错误！");

        if (a == true && b == true) {

            System.out.println("登录成功");

            Database logotime = new Database();
            logotime.logotime();

        } else {

            System.out.println("登录失败！");
            System.exit(0);

        }
    }


    public void question(String question) {                   //被Uer调用， 模拟功能：提取问题关键字

        int i;

        String questi = null;
        String questio = null;

        String a = null;
        String b = null;
        String c = null;
        String d = null;

        String e = null;
        String f = null;
        String g = null;
        String h = null;
        String que = null;

        String question2 = new String(question);

        String[] quest = question2.split("");

        for (i = 0; i < quest.length; i++) {
            if (quest[i].equals("蓝")) {
                questi = quest[i];
            }

            if (quest[i].equals("屏")) {
                questio = quest[i];
            }

            que = questi + questio;
            if (que.equals("蓝屏")) {
                break;

            }

            que = null;

            if (quest[i].equals("无")) {
                a = quest[i];
            }

            if (quest[i].equals("法")) {
                b = quest[i];
            }

            if (quest[i].equals("上")) {
                c = quest[i];
            }

            if (quest[i].equals("网")) {
                d = quest[i];
            }

            que = a + b + c + d;

            if (que.equals("无法上网")) {
                break;
            }

            que = null;

            if (quest[i].equals("电")) {
                e = quest[i];
            }

            if (quest[i].equals("脑")) {
                f = quest[i];
            }

            if (quest[i].equals("卡")) {
                g = quest[i];
            }

            if (quest[i].equals("顿")) {
                h = quest[i];
            }

            que = e + f + g + h;

            if (que.equals("电脑卡顿")) {
                break;
            }

            if (que.equals(""))
                que = "暂无内容";
        }

        switch (que) {
            case "蓝屏":
                System.out.println("您可以先尝试搜索关键词“蓝屏”");
                break;
            case "无法上网":
                System.out.println("您可以先尝试搜索关键词“无法上网”");
                break;
            case "电脑卡顿":
                System.out.println("您可以先尝试搜索关键词“电脑卡顿”");
                break;
            default:
                System.out.println("您的问题暂无相关数据，请您尝试搜索一下");
                break;
        }
    }


    public void research(String research) {    //被Uer调用， 模拟功能：已有问题的内容

        switch (research) {

            case "蓝屏":
                System.out.println("问题的解决方法：");
                System.out.println("第一步、我们先启动电脑，在系统启动画面快出现时按键盘上的“F8”快捷键，进入启动菜单选项，可以试着进入“安全模式”。");
                System.out.println("      如果成功进入，那么蓝屏的问题可能是由于安装某些程序（例如杀毒软件之类）或者驱动造成，把出故障前最后一次安装的程序或者驱动程序卸载掉重启系统，应该就可以正常进入了。");
                System.out.println("第二步、如果上述的方法无效，连安全模式都进不去一样的蓝屏，那么就需要借助一下启动U盘或者光盘上的PE系统来帮忙了，进入PE系统后，先扫描一下硬盘，检查下是否硬盘出现坏道造成蓝屏。");
                System.out.println("      通常硬盘本身故障的话也比较容易出现00000024故障，如果是检查到硬盘故障，建议更换或者在保修期的话尽早返回厂方保修。");
                System.out.println("第三步、这步其实最简单，如果检查硬盘并无故障的话，那就在PE系统里把重要的文件资料复制出来，重新安装过系统。");
                System.out.println("      一般如果不是硬盘故障的话，多是因为严重的驱动器碎片、超载的文件I/O、第三方的驱动器镜像软件或者一些防病毒软件出错造成的00000024故障，安装过系统基本可以解决问题。");
                break;

            case "无法上网":
                System.out.println("问题的解决方法：");
                System.out.println("1、在右下角的工具栏上，会有一个无线连接的图标，如果不能上网了，需要做的就是故障诊断和修复，直接在无线图标上点击鼠标左键。");
                System.out.println("2、在出现的目前连接的网络名上点击右键，然后选择“状态”。");
                System.out.println("3、选择里面的“诊断”，这个时候，系统会自动检测故障，并开始修复。");
                System.out.println("4、开始诊断故障，可能会持续大概一分钟。");
                System.out.println("5、诊断完毕并修复，成功之后会有这样的提示。");
                System.out.println("6、基本上的上网问题都可以通过系统自带的诊断工具解决。");
                break;

            case "电脑卡顿":
                System.out.println("问题的解决方法：");
                System.out.println("1、少开监控或不开监控，经常查杀病毒。");
                System.out.println("2、重装或升级驱动。");
                System.out.println("3、查杀一下木马还原一下系统或重装。");
                System.out.println("4、换用大功率风扇并清理灰尘。");
                System.out.println("5、用软件修复硬盘坏道，格式化硬盘重新分区重装，换硬盘。");
                System.out.println("6、用安全软件检测并修复。");
                System.out.println("7、清理垃圾和磁盘。");
                System.out.println("8、电脑卡一般有软硬引起的，软件、驱动、系统引起的好解决，因硬盘或硬件不兼容故障引起的不太好解决，找起故障来源也比较费劲，如果是这样最好是检修一下去。");
                break;

            default:
                System.out.println("暂无有关内容");
                System.out.println("请查看经验库，或者向人工咨询");

        }

    }

}



